﻿using System;
using AppDomainToolkit;
using ZilLion.Core.ConfigManager.Manager;
using ZilLion.TaskManager.Core.Quartz;
using ZilLion.TaskManager.Entity;
using ZilLion.TaskManager.Entity.Config;
using ZilLion.TaskManager.Entity.Entity;

namespace ZilLion.TaskManager.Core.TaskRunTime.AppDomain
{
    [Serializable]
    public class TaskAppDomainExcuteProvider : MarshalByRefObject
    {
        private static TaskAppDomainExcuteProvider _taskAppDomainExcuteProvider;

     
        private TaskAppDomainExcuteProvider()
        {
        }


        public static TaskAppDomainExcuteProvider GetInstance()
        {
            if (_taskAppDomainExcuteProvider == null)
                _taskAppDomainExcuteProvider = new TaskAppDomainExcuteProvider();
            return _taskAppDomainExcuteProvider;
        }

        /// <summary>
        ///     开始任务
        /// </summary>
        /// <param name="config"></param>
        /// <param name="taskModuleConfig"></param>
        /// <returns></returns>
        public void StartTask(TaskConfig config, TaskModuleConfigEntity taskModuleConfig)
        {
            //获取任务配置模块
            var appDomainContext = TaskAppDomainRuntimePool.GetInstance().GetAppDomainContext(taskModuleConfig.Taskmodulepackagekey);
            if (appDomainContext == null) //如果appdomain没有启动，则需要启动并初始化InitScheduler
            {
                TaskAppDomainRuntimePool.GetInstance().Add(taskModuleConfig.Taskmodulepackagekey);
                appDomainContext = TaskAppDomainRuntimePool.GetInstance().GetAppDomainContext(taskModuleConfig.Taskmodulepackagekey);
                RemoteAction.Invoke(appDomainContext.Domain, config,  taskModuleConfig,(taskconfig, moduleConfig) =>
                {
                    var container = AppDomainSchedulerContainer.GetContainerInstance();
                    container.InitScheduler(taskconfig, moduleConfig);
                    container.StartScheduler(); //开始StartScheduler
                });
            }


            RemoteAction.Invoke(appDomainContext.Domain, config, taskconfig =>
            {
                var container = AppDomainSchedulerContainer.GetContainerInstance();
                container.StartTask(taskconfig); //开始任务
            });
        }

        /// <summary>
        ///     暂停任务
        /// </summary>
        /// <returns></returns>
        public void PauseTask(TaskConfig config, TaskModuleConfigEntity taskModuleConfig)
        {
            var appDomainContext = TaskAppDomainRuntimePool.GetInstance().GetAppDomainContext(taskModuleConfig.Taskmodulepackagekey);

            if (appDomainContext != null) //如果appdomain没有启动，则需要启动并初始化InitScheduler
            {
                RemoteAction.Invoke(appDomainContext.Domain, config, taskconfig =>
                {
                    var container = AppDomainSchedulerContainer.GetContainerInstance();
                    container.StopTask(taskconfig);
                });
            }


           
        }

        /// <summary>
        ///     卸载任务
        /// </summary>
        /// <param name="config"></param>
        /// <param name="taskModuleConfig"></param>
        /// <returns></returns>
        public bool DeleteTask(TaskConfig config, TaskModuleConfigEntity taskModuleConfig)
        {
            var appDomainContext = TaskAppDomainRuntimePool.GetInstance().GetAppDomainContext(taskModuleConfig.Taskmodulepackagekey);

            if (appDomainContext != null) //如果appdomain没有启动，则需要启动并初始化InitScheduler
            {
                RemoteAction.Invoke(appDomainContext.Domain, config, taskconfig =>
                {
                    var container = AppDomainSchedulerContainer.GetContainerInstance();
                    container.DeleteTask(config);
                });
            }
          

            return true;
        }

        /// <summary>
        ///     释放任务资源
        /// </summary>
        /// <param name="taskModule"></param>
        /// <returns></returns>
        public bool UninstallSchedule(string taskModule, TaskModuleConfigEntity taskModuleConfig)
        {
            var appDomainContext = TaskAppDomainRuntimePool.GetInstance().GetAppDomainContext(taskModuleConfig.Taskmodulepackagekey);

            if (appDomainContext == null) return true; //如果appdomain没有启动，可以无视
            RemoteAction.Invoke(appDomainContext.Domain, () =>
            {
                var container = AppDomainSchedulerContainer.GetContainerInstance();
                container.StopSchedule();
            });
            return true;
        }
    }
}